package com.lgr.store.controller;


import com.lgr.store.service.CharactersService;
import com.lgr.store.service.DepService;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.CharEmpVo;
import com.lgr.store.vo.CharactersVo;
import com.lgr.store.vo.CharmoduleVo;
import com.lgr.store.vo.ModuleVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//权限设置
@Controller
@RequestMapping(value = "/admin/characters")
public class CharactersController {
    @Resource
    private CharactersService charactersService;
    @Resource
    private DepService depService;
    @Resource
    private HttpSession session;
    //查询角色
    @RequestMapping(value = "/list")
    private String list(PageObject pager, Model model){
        pager.setTotalRows(charactersService.getCount());
        pager.setDatas(charactersService.list(pager));
        model.addAttribute("pager",pager);
        return "characters";
    }
    //新增角色
    @RequestMapping(value = "/save")
    @ResponseBody
    private Map save(CharactersVo characters){
        charactersService.save(characters);
        Map<String,Object> maps = new HashMap<String, Object>();
        return maps;
    }
    //根据id查询角色
    @RequestMapping(value = "/getById")
    @ResponseBody
    private CharactersVo getById(int characterId){
        return charactersService.getById(characterId);
    }
    //删除角色
    @RequestMapping(value = "/del")
    @ResponseBody
    private Map del(int characterId){
        charactersService.del(characterId);
        Map<String,Object> maps = new HashMap<String, Object>();
        return maps;
    }

    //获取角色权限
    @RequestMapping(value = "/charactersAdd")
    public String charactersAdd(int characterId,Model model){
        //获取所有系统权限
        List<ModuleVo>   moduleRootList = charactersService.selectModule();
        List root=new ArrayList();
        for(ModuleVo module:moduleRootList){
            //查找根路径
            if(module.getRoot()==0){
                //查找下级
                List moduleList2=new ArrayList();
                for(ModuleVo module2:moduleRootList){
                    if(module2.getRoot()==module.getModuleId()){
                        moduleList2.add(module2);
                        //查找下级
                        List moduleList3=new ArrayList();
                        for(ModuleVo module3:moduleRootList){
                            if(module3.getRoot()==module2.getModuleId()){
                                moduleList3.add(module3);
                            }
                        }
                        module2.setModuleList2(moduleList3);
                    }
                }
                module.setModuleList2(moduleList2);
                root.add(module);
            }

        }

        model.addAttribute("characterId",characterId);//角色id
        model.addAttribute("root",root);//系统权限
        model.addAttribute("charmoduleList",charactersService.selectCharmodule(characterId));//角色权限
        return "charactersAdd";
    }
    //保存角色权限
    @RequestMapping(value = "/charactersSave")
    @ResponseBody
    private Map charactersSave(String ids,int characterId){
        List<CharmoduleVo> list=new ArrayList<CharmoduleVo>();
        if(ids!=null && !ids.equals("")){
            String id[]=ids.split(",");
            for (int i = 0; i <id.length ; i++) {
                if(!id[i].equals("")){
                    int key=Integer.parseInt(id[i]);
                    CharmoduleVo charmodule=new CharmoduleVo();
                    charmodule.setCharacterId(characterId);
                    charmodule.setModuleId(key);
                    list.add(charmodule);
                }
            }
        }
        charactersService.saveCharmodule(list,characterId);
        Map<String,Object> maps = new HashMap<String, Object>();
        return maps;

    }
    //保存用户授权
    @RequestMapping(value = "/charEmpSave")
    @ResponseBody
    private Map charEmpSave(String ids,int characterId){
        List<CharEmpVo> list=new ArrayList<CharEmpVo>();
        if(ids!=null && !ids.equals("")){
            String id[]=ids.split(",");
            for (int i = 0; i <id.length ; i++) {
                CharEmpVo charEmp=new CharEmpVo();
                charEmp.setEmpId(id[i]);
                charEmp.setCharacterId(characterId);
                list.add(charEmp);
            }
        }
        charactersService.saveCharEmp(list,characterId);
        Map<String,Object> maps = new HashMap<String, Object>();
        return maps;

    }
    @RequestMapping(value = "/charactersEmp")
    public String charactersEmp(int characterId,Model model){
        //查找部门
        model.addAttribute("depList",depService.list());
        model.addAttribute("empList",charactersService.findEmpDep());
        model.addAttribute("charactersEmpList",charactersService.findCharEmp(characterId,0));
        model.addAttribute("characterId",characterId);
        return "charactersEmp";
    }
}
